package com.example.lianjuan.Presenter.impl.impl;

import com.example.lianjuan.Model.API;
import com.example.lianjuan.Model.domain.OnSellPager;
import com.example.lianjuan.Presenter.impl.IOnSellPresenter;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.RetrofitManang;
import com.example.lianjuan.View.IOnSellCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class OnSellPresenterImpl implements IOnSellPresenter {

    private IOnSellCallback mCallback;
    private static final Integer DEFAULT_PAGE = 1;
    private int mCurrentPage = DEFAULT_PAGE;
    private OnSellPager mResult;
    private boolean IsLoading = false;

    @Override
    public void getOnSellContent() {
        if (IsLoading) {
            return;
        }
        IsLoading = true;
        if (mCallback != null) {
            mCallback.onLoadLoading();
        }
        Call<OnSellPager> task = getOnSellPagerCall(mCurrentPage);
        task.enqueue(new Callback<OnSellPager>() {
            @Override
            public void onResponse(Call<OnSellPager> call, Response<OnSellPager> response) {
                int code = response.code();
                IsLoading = false;
                LogUtils.i(OnSellPresenterImpl.this, "code---->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    mResult = response.body();
                    LogUtils.d(OnSellPresenterImpl.this, "reSult---->" + mResult.toString());
                    handlerOnLoadedSuccess();
                } else {
                    handlerOnLoadedError();
                }
            }

            @Override
            public void onFailure(Call<OnSellPager> call, Throwable t) {
                IsLoading = false;
                LogUtils.i(OnSellPresenterImpl.this, "t---->" + t.toString());
                handlerOnLoadedError();
            }
        });
    }

    private Call<OnSellPager> getOnSellPagerCall(int currentPage) {
        Retrofit retrofit = RetrofitManang.getRetrofitManang().getRetrofit();
        API api = retrofit.create(API.class);
        Call<OnSellPager> task = api.getOnSellContent(getUrl(currentPage));
        return task;
    }

    @Override
    public void reLoad() {
        //重新加载
        mCurrentPage = DEFAULT_PAGE;
        getOnSellContent();
    }

    @Override
    public void loadMore() {
        mCurrentPage++;
        if (IsLoading) {
            return;
        }
        IsLoading = true;
        Call<OnSellPager> task = getOnSellPagerCall(mCurrentPage);
        task.enqueue(new Callback<OnSellPager>() {
            @Override
            public void onResponse(Call<OnSellPager> call, Response<OnSellPager> response) {
                int code = response.code();
                IsLoading = false;
                LogUtils.i(OnSellPresenterImpl.this, "code---->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    mResult = response.body();
                    LogUtils.d(OnSellPresenterImpl.this, "reSult---->" + mResult.toString());
                    //有没有更多内容
                    isMoreLadedContent(mResult);
                } else {
                    handlerMoreLoadedError();
                }
            }

            @Override
            public void onFailure(Call<OnSellPager> call, Throwable t) {
                IsLoading = false;
                LogUtils.i(OnSellPresenterImpl.this, "t---->" + t.toString());
                handlerMoreLoadedError();
            }
        });
    }

    private void isMoreLadedContent(OnSellPager result) {
        if (mCallback != null) {
            if (!isEmpty(result)) {
                mCallback.onMoreLoadedContent(result);
            } else {
                mCallback.onMoreLoadedEmpty();
            }
        }
    }

    private boolean isEmpty(OnSellPager result) {
        try {
            int size = result.getData().getTbk_dg_optimus_material_response().getResult_list().getMap_data().size();
            return size == 0;
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        }
    }

    private void handlerMoreLoadedError() {
        mCurrentPage--;
        if (mCallback != null) {
            mCallback.onMoreLoadedError();
        }
    }

    private void handlerOnLoadedError() {
        if (mCallback != null) {
            mCallback.onLoadError();
        }
    }

    private void handlerOnLoadedSuccess() {
        if (mCallback != null) {
            mCallback.onLoadedSuccessContent(mResult);
        }
    }

    @Override
    public void registerViewCallback(IOnSellCallback callback) {
        this.mCallback = callback;
    }

    @Override
    public void unregisterViewCallback(IOnSellCallback callback) {
        this.mCallback = null;
    }

    private String getUrl(int currentPage) {
        return "onSell/" + currentPage;
    }

}
